# coding:utf-8
'''
Created on 2015年6月26日

@author: Emrys
'''


class Person:

    def __init__(self, name):
        self.name = name

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        if not isinstance(value, str):
            raise TypeError('Excepected a string')
        self._name = value

    @name.deleter
    def name(self):
        raise AttributeError("Can't delete attribute")


class SubPerson(Person):

    @property
    def name(self):
        print('Getting name ')
        return super().name

    @name.setter
    def name(self, value):
        print('SubPerson Setting name to', value)
        super(SubPerson, SubPerson).name.__set__(self, value)

    @name.deleter
    def name(self):
        print('SubPeron Deleting name')
        super(SubPerson, SubPerson).name.__delattr__(self)


class IntValue:

    def __init__(self, name):
        self.name = name

    #__get__,__set__,__delete__
    # 这些方法用在当一个对象的某个属性是该类的实例的时候， 当对该
    # 元素读值，赋值，删除值的时候调用这几个元素
    def __get__(self, instance, cls):
        print("IntValue __get__")
        if instance is None:
            return self

    def __set__(self, instance, value):
        print("IntValue __set__")
        if not isinstance(value, int):
            raise TypeError('Expected an int')
        instance.__dict__[self.name] = value

    def __delete__(self, instance):
        print("IntValue __Delete__")
        del instance.__dict__[self.name]


class Point:
    # 类的属性
    x = IntValue("X")
    y = IntValue("Y")

    def __init__(self, x, y):
        # 如果放到初始化这里是没有用的
        #         x = IntValue("X")
        #         y = IntValue("Y")
        self.x = x
        self.y = y


p = Point(1, 2)


# s = SubPerson('Guido')
# print(s.name)
# s.name = 'Larry'
